//
//  MemberService.m
//  shsartist
//
//  Created by liwei on 16/3/21.
//  Copyright © 2016年 liwei. All rights reserved.
//

#import "MemberService.h"
#import "SQLManager.h"

@interface MemberService() {
    FMDatabaseQueue *_dbQueue;
}

@end

@implementation MemberService

#pragma mark 初始化数据库操作，返回数据库操作实例
- (MemberService *)init {
    if (self = [super init]) {
        _dbQueue = [[SQLManager sharedInstance] dbQueue];
    }
    return self;
}

#pragma mark 插入新的数据，会替换原来的数据
-(void)insert:(SBMember *)member {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_dbQueue inDatabase:^(FMDatabase *db) {
            [db executeUpdate:@"delete from NAMember"];
           BOOL isSuccess= [db executeUpdate:@"insert into NAMember (AccessToken, RefreshToken, UserId, NickName, SpaceBackGround, Portrait, Sex, Birthday, Mobile, QQ, Weixin, AreaCode, ProvinceName, CityName, DistrictsName, IsAuthentication, IsMember, IsBindingStudent, HxTag, IsTeacher, IsVisitor) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", member.AccessToken, member.RefreshToken, member.UserId, member.NickName, member.SpaceBackGround, member.Portrait, member.Sex, member.Birthday, member.Mobile, member.QQ, member.Weixin, member.AreaCode, member.ProvinceName, member.CityName, member.DistrictsName, [NSString stringWithFormat:@"%d",member.IsAuthentication],[NSString stringWithFormat:@"%d",member.IsMember], [NSString stringWithFormat:@"%d",member.IsBindingStudent], member.HxTag, [NSString stringWithFormat:@"%d",member.IsTeacher], [NSString stringWithFormat:@"%d",member.IsVisitor]];
            DeLog(@"-------DB_insert_isSuccess:%d",isSuccess);
        }];
      
    });
}
-(void)update:(SBMember *)member{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_dbQueue inDatabase:^(FMDatabase *db) {
           BOOL isSuccess=[db executeUpdate:@"update NAMember set NickName=?, SpaceBackGround=?, Portrait=?, Sex=?, Birthday=?, Mobile=?, QQ=?, Weixin=?, AreaCode=?, ProvinceName=?, CityName=?, DistrictsName=?, IsAuthentication=?, IsMember=?, IsBindingStudent=?, HxTag=?, IsTeacher=?, IsVisitor=? where UserId=?", member.NickName, member.SpaceBackGround, member.Portrait, member.Sex, member.Birthday, member.Mobile, member.QQ, member.Weixin, member.AreaCode, member.ProvinceName, member.CityName, member.DistrictsName, [NSString stringWithFormat:@"%d",member.IsAuthentication],[NSString stringWithFormat:@"%d",member.IsMember], [NSString stringWithFormat:@"%d",member.IsBindingStudent], member.HxTag, [NSString stringWithFormat:@"%d",member.IsTeacher],[NSString stringWithFormat:@"%d",member.IsVisitor],member.UserId];
              DeLog(@"-------DB_update_isSuccess:%d",isSuccess);
        }];
    });
}
#pragma mark 查询最新插入的Member
- (void)queryLastOneWithCallBack:(void (^)(SBMember *))block {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        __block SBMember *member = nil;
        [_dbQueue inDatabase:^(FMDatabase *db) {
            FMResultSet *rs = [db executeQuery:@"SELECT * FROM NAMember order by _id desc limit 1"];
            if ([rs next]) {
                member = [[SBMember alloc] init];
                
                //member.Token = [rs stringForColumn:@"Token"];
                member.AccessToken = [rs stringForColumn:@"AccessToken"];
                member.RefreshToken = [rs stringForColumn:@"RefreshToken"];
                
                member.UserId = [rs stringForColumn:@"UserId"];
                member.NickName = [rs stringForColumn:@"NickName"];
                member.SpaceBackGround = [rs stringForColumn:@"SpaceBackGround"];
                member.Portrait = [rs stringForColumn:@"Portrait"];
                member.Sex = [rs stringForColumn:@"Sex"];
                member.Birthday = [rs stringForColumn:@"Birthday"];
                
                member.Mobile = [rs stringForColumn:@"Mobile"];
                member.QQ = [rs stringForColumn:@"QQ"];
                member.Weixin = [rs stringForColumn:@"Weixin"];
                
                member.AreaCode = [rs stringForColumn:@"AreaCode"];
                member.ProvinceName = [rs stringForColumn:@"ProvinceName"];
                member.CityName = [rs stringForColumn:@"CityName"];
                member.DistrictsName = [rs stringForColumn:@"DistrictsName"];
                
                member.IsAuthentication = [[rs stringForColumn:@"IsAuthentication"] intValue];
                member.IsMember = [[rs stringForColumn:@"IsMember"] intValue];
                member.IsBindingStudent = [[rs stringForColumn:@"IsBindingStudent"] intValue];
                member.HxTag = [rs stringForColumn:@"HxTag"];
                
                member.IsTeacher = [[rs stringForColumn:@"IsTeacher"] intValue];
                member.IsVisitor = [[rs stringForColumn:@"IsVisitor"] intValue];
            }
            [rs close];
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            block(member);
        });
    });
}
#pragma mark 查询最近登录的手机号或者邮箱
- (void)queryLastMemberMobileOrEmailWithCallBack:(void (^)(NSString *))block {
    [self queryLastOneWithCallBack:^(SBMember *member) {
        NSString *account = member.Mobile;
        block(account);
    }];
}

#pragma mark 退出登录
- (void)logout {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_dbQueue inDatabase:^(FMDatabase *db) {
            [db executeUpdate:@"delete from NAMember"];
        }];
    });
}

@end
